package class61To70;
import java.io.*;
import java.util.Arrays;


// https://www.luogu.com.cn/problem/P3366
public class PrimAndHeap {

    public static int n, m, u, v, w;

    public static final int MAX_N = 5001;

    public static final int MAX_M = 400005;

    public static int[] head = new int[MAX_N];

    public static int[] next = new int[MAX_M];

    public static int[] to = new int[MAX_M];

    public static int[] weight = new int[MAX_M];

    public static int cnt;

    // 改写的堆结构 存储的是节点编号和节点权值
    public static int[][] heap = new int[MAX_N][2];

    public static int heapSize;


    // where[v] = -1，表示v这个节点，从来没有进入过堆
    // where[v] = -2，表示v这个节点，已经弹出过了
    // where[v] = i(>=0)，表示v这个节点，在堆上的i位置
    public static int[] where = new int[MAX_N];

    // 当前已经访问的节点个数
    public static int nodeCnt;

    public static void build() {
        heapSize = 0;
        nodeCnt = 0;
        cnt = 1;
        Arrays.fill(head, 0, n + 1, 0);
        Arrays.fill(where, 0, n + 1, -1);
    }

    public static void addEdge(int u, int v, int w) {
        next[cnt] = head[u];
        to[cnt] = v;
        weight[cnt] = w;
        head[u] = cnt++;
    }

    public static void heapInsert(int i) {
        while (heap[i][1] < heap[(i - 1) / 2][1]) {
            swap(i, (i - 1) / 2);
            i = (i - 1) / 2;
        }
    }

    public static boolean isEmpty() {
        return heapSize == 0;
    }

    public static void pop() {
        u = heap[0][0];
        w = heap[0][1];
        swap(0, --heapSize);
        heapIfy(0);
        where[u] = -2;
        nodeCnt++;
    }

    private static void swap(int i, int j) {
        int a = heap[i][0];
        int b = heap[j][0];
        where[a] = j;
        where[b] = i;
        int[] tmp = heap[i];
        heap[i] = heap[j];
        heap[j] = tmp;
    }

    public static void heapIfy(int i) {
        int left = i * 2 + 1;
        while (left < heapSize) {
            int best = left + 1 < heapSize && heap[left + 1][1] < heap[left][1] ? left + 1 : left;
            best = heap[best][1] < heap[i][1] ? best : i;
            if (best == i) {
                break;
            }
            swap(best, i);
            i = best;
            left = i * 2 + 1;
        }
    }

    public static void addOrUpdateOrIgnore(int ei) {
        int v = to[ei];
        int w = weight[ei];
        // 去往v点，权重w
        if (where[v] == -1) {
            // v这个点，没有被访问过，可以加入堆中
            heap[heapSize][0] = v;
            heap[heapSize][1] = w;
            where[v] = heapSize++;
            heapInsert(where[v]);
        } else if (where[v] >= 0) {
            // v这个点的记录，在堆上的位置是where[v]
            heap[where[v]][1] = Math.min(heap[where[v]][1], w);
            heapInsert(where[v]);
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            n = (int) in.nval;
            in.nextToken();
            m = (int) in.nval;
            build();
            for (int i = 0; i < m; i++) {
                in.nextToken();
                u = (int) in.nval;
                in.nextToken();
                v = (int) in.nval;
                in.nextToken();
                w = (int) in.nval;
                addEdge(u, v, w);
                addEdge(v, u, w);
            }
            int ans = prim();
            out.println(nodeCnt == n ? ans : "orz");
        }
        out.flush();
        out.close();
        br.close();
    }

    public static int prim() {
        // 从节点1出发
        nodeCnt = 1;
        where[1] = -2;
        for (int ei = head[1]; ei > 0; ei = next[ei]) {
            addOrUpdateOrIgnore(ei);
        }
        int ans = 0;
        while (!isEmpty()) {
            pop();
            ans += w;
            for (int ei = head[u]; ei > 0; ei = next[ei]) {
                addOrUpdateOrIgnore(ei);
            }
        }
        return ans;
    }
}
